package com.software.backend.searchmovie.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @program: back-end
 * @description:
 * @author: C@t
 * @create: 2023-04-18 08:47
 **/
public class test {
    public String videoSpit(String path, Long startTime, int duration) throws IOException {
        int i = (int) (startTime / 1000);
        int i1 = duration / 1000;
        String ffmpegExePath = "D:\\yoki\\downloads\\ffmpeg-5.1.2-essentials_build\\bin" + "\\ffmpeg.exe";
        List<String> command = new ArrayList<>();
        //ffmpeg -i video.mp4 -ss 00:00:00 -t 00:00:10 -c copy -threads 0 output1.mp4
        command.add(ffmpegExePath);
        command.add("-i");
        command.add(path);
        command.add("-ss");
        command.add(String.valueOf(i));
        command.add("-t");
        command.add(String.valueOf(i1));
//        command.add("-accurate_seek");
        command.add("-c");
        command.add("copy");
        command.add("-threads");
        command.add("0");

        if (!FileUtil.file(path).isFile()) {
            return "文件不存在，请检查！！";
        }
//        command.add(path);
//        command.add("-codec");
//        command.add("copy");
//        command.add("-avoid_negative_ts");
//        command.add("1");
        command.add(path);
//        command.add("-c:v");
//        command.add("libx264");
//        command.add("-c:a");
//        command.add("aac");
//        command.add("-strict");
//        command.add("experimental");
        String s = IdUtil.simpleUUID();
        //本地开发写死路径，这个到时候就写到app的其中一个位置就可以
        String substring = path.substring(path.lastIndexOf("."));
        String e1 = "D:\\data\\split\\" + s + path.substring(path.lastIndexOf("."));
        StringBuilder stringBuffer = new StringBuilder();
        //业务需求，判断是否为mp4，如不需要可以去掉
        if (!"mp4".equals(substring)) {
            String s1 = transCoding(e1);
            command.add(s1);
            stringBuffer.append(s1);
        }else {
            command.add(e1);
            stringBuffer.append(e1);
        }
        try {
//            System.out.println(command);
            ProcessBuilder builder = new ProcessBuilder();

            builder.command(command);
            //正常信息和错误信息合并输出

            builder.redirectErrorStream(true);

            //开始执行命令

            Process process = builder.start();
            //如果你想获取到执行完后的信息，那么下面的代码也是需要的

            StringBuilder sbf = new StringBuilder();

            String line;

            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

            while ((line = br.readLine()) != null) {

                sbf.append(line);

                sbf.append(" ");
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            e.printStackTrace();

        }
        return "操作失败";
    }
    public  String transCoding(String path) throws IOException {
        String ffmpegExePath = "D:\\yoki\\downloads\\ffmpeg-5.1.2-essentials_build\\bin" + "\\ffmpeg.exe";
        ArrayList<String> command = new ArrayList<>();
        command.add(ffmpegExePath);
        command.add("-i");
        command.add(path);
        command.add("-vcodec");
        command.add("copy");
        command.add("-f");
        command.add("mpegts");
        String s = "D:\\data\\split\\" + UUID.randomUUID() + ".mp4";
        command.add(s);
        // 执行操作
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(command);
        builder.redirectErrorStream(true);
        Process process = builder.start();
        return s;
    }
}
